package top.eggcode.common.consts;


import java.io.Serializable;

/**
 * 处理状态
 *
 * @author huangxunhui
 * Date: Created in 18/8/29 上午9:54
 * Utils: Intellij Idea
 * Description:
 * 1000～1999 客户端请求错误，前缀：REQ_
 * 2000～2999 用户操作错误，前缀：USER_
 * 3000～3999 业务处理错误，前缀：SYSTEM_
 */
public enum ResponseStatus implements Serializable {

    // 成功
    OK(200, "成功"),

    /**
     * 请求错误
     */
    USER_UNAUTHORIZED(2001, "操作未授权"),
    USER_UNAUTHENTICATED(2002, "身份未认证"),
    USER_INVALID_TOKEN(2003, "非法Token"),
    USER_PASSWORD_ERROR(2004, "密码错误"),
    USER_ACCOUNT_NOT_FOUND(2005, "用户名不存在"),
    USER_OVERDUE_TOKEN(2006, "Token 过期"),
    USER_AUTHORIZATION_FAIL(2007, "身份认证失败"),
    USER_TOKEN_REFRESH(2008, "Token 需要刷新"),
    USER_NO_NEED_REFRESH(2009, "Token 状态正常，不需要刷新"),
    USER_USERNAME_REPETITION(2010, "用户名重复"),
    REQ_FORMAT_ERROR(2011, "参数格式错误"),
    REQ_ILLEGAL_CHARACTER(2012, "非法字符"),
    REQ_ARGUMENT_TYPE_MISMATCH(2013, "参数类型不匹配"),
    REQ_METHOD_NOT_SUPPORT(2014, "请求方式不支持"),
    REQ_NO_TOKEN(2015, "未携带 token"),
    REQ_ID_IS_EMPTY(2016, "ID 为空"),

    /**
     * 服务器错误
     */
    SYSTEM_EXCEPTION(3000, "发生系统异常"),
    SYSTEM_TIME_OUT(3002, "超时"),
    SYSTEM_ADD_ERROR(3003, "添加失败"),
    SYSTEM_UPDATE_ERROR(3004, "更新失败"),
    SYSTEM_DELETE_ERROR(3005, "删除失败"),
    SYSTEM_GET_ERROR(3006, "查找失败"),
    SYSTEM_NULL_POINTER(3007, "空指针异常"),
    SYSTEM_3008(3008, "非Web上下文无法获取Request"),
    SYSTEM_IO_EXCEPTION(3009, "IO 流操作中断"),
    SYSTEM_SHIRO_EXCEPTION(3010, "权限管理器异常"),
    SYSTEM_ACCOUNT_REPETITION(3011, "账户重复"),
    SYSTEM_TARGET_NOT_EXIST(3012, "目标不存在"),
    ;

    private final Integer code;

    private final String msg;


    ResponseStatus(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    /**
     * 通过状态码获取枚举对象
     *
     * @param code 状态码
     * @return 枚举对象
     */
    public static ResponseStatus getMessage(int code) {
        for (ResponseStatus responseStatus : ResponseStatus.values()) {
            if (code == responseStatus.getCode()) {
                return responseStatus;
            }
        }
        return null;
    }

}

